#include "uzonlineprovider.h"
#include <QDebug>
#include <QNetworkReply>
#include <QEventLoop>

const QString UzonlineProvider::authUrl("https://cabinet.uzonline.uz/auth.do");
const QString UzonlineProvider::cabinetUrl("https://cabinet.uzonline.uz/cabinet.do");


UzonlineProvider::UzonlineProvider(QObject *parent) :
        AbstractProvider(parent)
{
}

void UzonlineProvider::setData(const QHash<QString, QString> &dataHash)
{
    authDataHash["login"] = dataHash["login"];
    authDataHash["password"] = dataHash["password"];
}

/*
 UzonlineProvider::AuthResult  UzonlineProvider::syncLogin()
{
    authorize(authDataHash["login"], authDataHash["password"]);
    QEventLoop loop;
    QObject::connect(this, SIGNAL(status(UzonlineProvider::AuthResult)), &loop, SLOT(quit()));
    loop.exec();

    return authResult;
}
*/

void UzonlineProvider::authorize()
{
    QByteArray postData = QString("state=0&login=%1&password=%2").arg(authDataHash["login"], authDataHash["password"]).toUtf8();
    QNetworkReply *reply = manager->post( QNetworkRequest( QUrl( authUrl ) ), postData);
    connect(reply, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(sslErrorsSlot(QList<QSslError>)));
    connect(reply, SIGNAL(finished()), this, SLOT(authorizeSlot()));
    //connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(replyErrorSlot(QNetworkReply::NetworkError)));
}

void UzonlineProvider::authorizeSlot()
{
    QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
    QString content = reply->readAll();

    if(reply->error() != QNetworkReply::NoError) {
        authResult = NetworkError;
    }
    else if(QRegExp("Invalid Login\\/Password").indexIn(content) != -1) {
        authResult = AuthError;
    }
    else if(QRegExp("cabinet\\.do").indexIn( reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl().toString() ) != -1) {
        authResult = AuthSuccess;
    }
    else {
        authResult = Unknown;
    }
    reply->deleteLater();
    qDebug() << authResult;
    emit status(authResult);
}

void UzonlineProvider::sslErrorsSlot(const QList<QSslError> &errors)
{
    QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
    reply->ignoreSslErrors();
    foreach (const QSslError &error, errors) {
        qDebug() << error.errorString();
    }

}

void UzonlineProvider::requestData()
{
    QNetworkReply *reply = manager->get( QNetworkRequest( QUrl( cabinetUrl ) ));
    connect(reply, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(sslErrorsSlot(QList<QSslError>)));
    connect(reply, SIGNAL(finished()), this, SLOT(requestDataSlot()));
}

void UzonlineProvider::requestDataSlot()
{
    QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
    QString content = reply->readAll();

    QHash<QString, QString> infoHash;

    QRegExp number("<img src=\"img/active_user\\.png\"/>\\s+<div class=\"middle\">(.+)</div>");
    number.setMinimal(true);
    if(number.indexIn(content) != -1) {
        infoHash["number"] = number.cap(1);
    }

    emit information(infoHash);
}
